import { ERPExcelWorkbook } from './assets/js/excel.js';


export class excelInfo {
    constructor(data) {
        this.json_columns = data?.json_columns;
        this.json_data = data?.json_data;

        this.workbook = new ERPExcelWorkbook();
        this.workbook.excelExcelWorkbookName = data?.fileName;;
    };

    initDom = () => {
        if (!this.json_columns) {
            this.json_columns = Object.keys(this.json_data[0]);
        }

        // this.workbook.excelExcelWorkbookName = this.fileName;
        const sheet = this.workbook.addSheet(); //添加一页Excel
        this.addTitle(sheet);
        this.addTableData(sheet);
    };

    excelExport = (callback) => {
        this.workbook.excelExport(isDown => {
            console.log(isDown)
            callback(isDown);
        })
    }

    // const row = sheet.addRow(); //add 行
    setRowData = (cell, cellIndex, dataX, dataY) => {
        // { text: '' }
        // { text: '', image: dataY.pictures }
        if (
            dataY[dataX] &&
            typeof dataY[dataX] === 'string' &&
            (
                dataY[dataX].indexOf('.png') > -1 ||
                dataY[dataX].indexOf('.jpg') > -1 ||
                dataY[dataX].indexOf('.jpeg') > -1 ||
                dataY[dataX].indexOf('.gif') > -1
            )
        ) {
            cell.value = { text: '', image: dataY[dataX] };
        } else {
            cell.value = { text: dataY[dataX] };
        }
    }

    addTitle = (sheet) => {
        const row = sheet.addRow();
        for (let x = 0; x < this.json_columns.length; ++x) {
            const dataX = this.json_columns[x];
            const cell = row.addCell();
            cell.value = { text: dataX };
        }
    };

    addTableData = (sheet) => {
        for (let y = 0; y < this.json_data.length; ++y) {
            let dataY = this.json_data[y];
            let row = sheet.addRow();
            console.log(row, 'row');
            for (let x = 0; x < this.json_columns.length; ++x) {
                let dataX = this.json_columns[x];
                let cell = row.addCell();
                this.setRowData(cell, x, dataX, dataY)
            }
        }
    };
}

if (window.layui && layui.define) {
    layui.define(["table"], function(exports) {
        var $ = layui.$;
        var table = layui.table;
        var Class = function() {
            this.options = {};
            this.init = function(options) {
                var that = this;
                that.options.table_id = options.table_id || "";
                that.options.table_title = options.table_title || new Date().getTime();
                that.options.table_data = options.table_data || [];
                that.options.table_columns = options.table_columns;
                return that;
            };
            this.tableExport = function() {
                var index2 = layer.load();
                var that = this;
                excel = new excelInfo({
                    fileName: that.options.table_title,
                    json_data: that.options.table_data,
                    json_columns: that.options.table_columns
                });
                excel.initDom();
                excel.excelExport((isDown) => {
                    layer.close(index2);
                });
            };
        };
        var table2excel = function () {
            this.init = function(options, auto_export) {
                var excel2 = new Class().init(options);
                if (auto_export) {  //原始数据导出
                    excel2.tableExport();
                } else {
                    if (excel2) {
                        $(".layui-table-tool-self [lay-event=LAYTABLE_EXPORT]").click(function (e) {
                            setTimeout(function () {
                                $(".layui-table-tool-panel li[data-type=xls]").unbind('click').click(function () {
                                    var that = excel2;
                                    var id = that.options.table_id;
                                    var old_data = that.options.table_data || [];
                                    var json_data = [];
                                    json_data = $.extend(true, old_data, json_data);
                                    if (!json_data.length && id) {
                                        var table_data = table.cache[id];
                                        layui.each(table_data, function(i1, item1) {
                                            var tmp = {};
                                            table.eachCols(id, function(i3, item3) {
                                                if (item3.field && item3.type == "normal") {
                                                    if (item3.hide) {
                                                        return;
                                                    }
                                                    var content = item1[item3.field];
                                                    if (content && typeof content === "string" && (content.indexOf(".png") > -1 || content.indexOf(".jpg") > -1 || content.indexOf(".jpeg") > -1 || content.indexOf(".gif") > -1)) {
                                                        tmp[item3.title] = content;
                                                    } else {
                                                        tmp[item3.title] = content;
                                                        var templet  = item3.exportTemplet || item3.templet
                                                        if (typeof templet === 'function') {
                                                            tmp[item3.title] = templet(item1);
                                                        }
                                                    }
                                                }
                                            });
                                            if (!$.isEmptyObject(tmp)) {
                                                json_data.push(tmp);
                                            }
                                        });
                                    }
                                    if (id) {
                                        var table_columns = [];
                                        table.eachCols(id, function(i, v) {
                                            if (v.field && v.type == "normal") {
                                                if (!v.hide) {
                                                    table_columns.push(v.title);
                                                }
                                            }
                                        });
                                        if (table_columns.length) {
                                            that.options.table_columns = table_columns;
                                        }
                                        var table_data = table.cache[id];
                                        var totalNums = {};
                                        layui.each(table_data, function(i1, item1) {
                                            table.eachCols(id, function(i3, item3) {
                                                var field = item3.field || i3, content = item1[field];
                                                if (item3.totalRow) {
                                                    totalNums[item3.title] = (totalNums[item3.title] || 0) + (parseFloat(content) || 0);
                                                } else if (item3.totalRowText) {
                                                    totalNums[item3.title] = item3.totalRowText;
                                                }
                                            });
                                        });
                                        if (!$.isEmptyObject(totalNums)) {
                                            json_data.push(totalNums);
                                        }
                                    }
                                    console.log(json_data);
                                    if (json_data.length) {
                                        that.options.table_data = json_data;
                                        excel2.tableExport();
                                    } else {
                                        table.exportFile(id, null, 'xls')
                                    }
                                });
                            }, 200)
                        })
                    }
                }
            }
        }
        exports("table2excel", new table2excel());
    });
}

// json_data = [
// {
//     "name": "花金妹",
//         "address": "1177弄",
//             "照片地址": "https://img-blog.csdnimg.cn/20200204233620255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMzU5MDUx,size_16,color_FFFFFF,t_70",
//                 "照片长度": 1,
//                     "药名": " ",
//                         "联系方式": "13817930730",
//                             "门牌": 1,
//                                 "户室": 204
// },
        //     {
        //         "name": "花金妹",
        //         "address": "1177弄",
        //         // "照片地址": "https://pubuserqiniu.paperol.cn/158627462_20_q19_ZFVLhcH0UeXOQQSMVo9dw.png?attname=20_19_splicing.png",
        //         "照片地址": "https://img-blog.csdnimg.cn/20200204233620255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMzU5MDUx,size_16,color_FFFFFF,t_70",
        //         "照片长度": 1,
        //         "药名": " ",
        //         "联系方式": "13817930730",
        //         "门牌": 1,
        //         "户室": 204
        //     },
        //     {
        //         "name": "花金妹",
        //         "address": "1177弄",
        //         // "照片地址": "https://pubuserqiniu.paperol.cn/158627462_20_q19_ZFVLhcH0UeXOQQSMVo9dw.png?attname=20_19_splicing.png",
        //         "照片地址": "https://img-blog.csdnimg.cn/20200204233620255.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxMzU5MDUx,size_16,color_FFFFFF,t_70",
        //         "照片长度": 1,
        //         "药名": " ",
        //         "联系方式": "13817930730",
        //         "门牌": 1,
        //         "户室": 204
        //     },
        // ];

        // json_columns = Object.keys(json_data[0]);
